﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
namespace ModuleDuoc
{
    public partial class frmTongHopNhapThuoc : Base.frmTK_Base
    {
        public frmTongHopNhapThuoc(string nhanvien)
        {
            InitializeComponent();
            _nhanvien = nhanvien;
        }
        float tongtien = 0;
        float tonglaixuat = 0;
        float avtlaixuat = 0;
        float tongban = 0;
        DataTable _dt;
        string _nhanvien;

        public override void btnView_Click(object sender, EventArgs e)
        {
            string sql = "SELECT MAVACH,sohd,tenvt,ngaylap,solo,hansd,ten,soluong,dongianhap,soluong*dongianhap as tongtiennhap,";
            sql += " giaban,giaban*soluong as tongtienban,manhanvien,100*((giaban-dongianhap)/dongianhap) as laixuat";
            sql += " FROM thongke.view_TongHopNhap";
            sql += " where dongianhap >0 and ngaylap between '" + dtpTuNgay.Value.ToString("MM/dd/yyyy HH:mm:ss") + "' and '" + dtpDenNgay.Value.ToString("MM/dd/yyyy HH:mm:ss") + "'";

            DataTable dt = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvData.DataSource = dt;
            _dt = dt;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    tongtien += float.Parse(dt.Rows[i]["tongtiennhap"].ToString());
                    tonglaixuat += float.Parse(dt.Rows[i]["laixuat"].ToString());
                    tongban += float.Parse(dt.Rows[i]["tongtienban"].ToString());
                }
                this.txtTongNhap.Text = String.Format("{0:0,0}", tongtien);
                avtlaixuat = tonglaixuat / dt.Rows.Count;
                this.txtLaiXuat.Text  = avtlaixuat.ToString();
            } 
        }
        public override void btnExportReport_Click(object sender, EventArgs e)
        {
            base.btnExportReport_Click(sender, e);
            frmShowReport frm = new frmShowReport(2);
            frmShowReport.strC = frmShowReport.BC.TONGHOPNHAPHANG;
            frmShowReport._dt = _dt;
            frmShowReport._tungay = dtpTuNgay.Value.ToString("dd/MM/yyyy");
            frmShowReport._denngay = dtpDenNgay.Value.ToString("dd/MM/yyyy");
            frmShowReport._tongnhap = string.Format("{0:0,0}", tongtien) + " VNĐ";
            frmShowReport._tongban = string.Format("{0:0,0}", tongban) + " VNĐ";
            frmShowReport._laixuat = avtlaixuat + "%";
            frmShowReport._nhanvien = _nhanvien;
            frm.ShowDialog();
            
        }
        private void frmTongHopNhapThuoc_Load(object sender, EventArgs e)
        {
        }
    }
}
